package dao

import (
	"database/sql"
	"fmt"
	"github.com/jmoiron/sqlx"
	"log"
)
//
type StudentTable struct {
	Id         int    `db:"id"`
	Name       string `db:"name"`
	Nick       sql.NullString `db:"nick"`
	Country    sql.NullString `db:"country"`
	Province   sql.NullString `db:"province"`
	City       sql.NullString `db:"city"`
	ImgUrl     sql.NullString `db:"img_url"`
	Status     sql.NullInt64  `db:"status"`
	CreateTime sql.NullString `db:"create_time"`
}
//SQL_student查询所有
func SelectAllStudentDao(db sqlx.DB){
	var stds [] StudentTable
	db.Select(&stds, "SELECT * FROM student")
	for i, v := range stds {
		fmt.Printf("%d--->%v\n", i+1, v)
	}
}
//SQL_student查询单个用户信息
func SelectOneStudentDao(db sqlx.DB,user string){
	var s StudentTable
	sql_txt := fmt.Sprintf("SELECT * FROM student WHERE name='%s'",user)
	err := db.Get(&s, sql_txt)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(s)
}

//SQL_student插入信息
//@init upDateMap := make(map[string]interface{})
//@param upDateMap["Id","Name","Nick","Country","Province","City","ImgUrl","Status","Create_time"]
func InsertStudentDao(db sqlx.DB,upDateMap map[string]interface{}){
	tx := db.MustBegin()
	tx.NamedExec(`INSERT INTO student VALUES (:Id, :Name, :Nick, :Country, :Province, :City, :ImgUrl, :Status, :Create_time);`, upDateMap)
	//备选方案
	//tx.MustExec(`INSERT INTO student VALUES ('19','Jack', 'Jack', 'England', '', '', 'http://img2.imgtn.bdimg.com/it/u=3588772980,2454248748&fm=27&gp=0.jpg', '1', '2018-06-26 17:08:35');`)
	err := tx.Commit()
	if err != nil {
		log.Fatalln(err)
	}
}